<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet">
	<title>提交</title>
</head>
<body>

<div class="container mt-5 ml-5 mr-5 pl-5 pr-5" style="max-width: 800px;">

    <h1 class="text-center fw-bold my-5">提交文章</h1>

	<form method="post" id="submit-form" action="/wp-json/submit/articles">

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">获取数据</button>
            <input type="text" class="form-control" name="data-url" placeholder="获取数据网址" id="data-url">
            <button class="btn btn-primary" type="button" id="get-data">获取数据</button>
        </div>

        <input type="hidden" name="pass" value="<?php if(isset($_GET['pass'])) echo $_GET['pass']; ?>" id="pass">

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">标题</button>
		    <input type="text" name="title" placeholder="标题" class="form-control" id="title">
        </div>

        <select class="form-control mb-4" name="template" id="template">
            <option value="theme-post.php" selected>插件</option>
            <option value="article">文章</option>
        </select>

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">名称</button>
        <input type="text" name="product_name" placeholder="请输入名称" id="product_name" class="form-control">
        </div>

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">URI</button>
        <input type="text" name="permalink" placeholder="请输入网址" id="permalink" class="form-control">
        </div>

        <textarea name="short-description" placeholder="请输入简单介绍(元描述)" class="form-control mb-4" id="short_description"></textarea>

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">下载路径</button>
            <input type="text" name="download-uri" placeholder="下载路径" class="form-control" id="download_uri">
        </div>

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">官网</button>
             <input type="text" name="preview-url" placeholder="英文预览地址" class="form-control" id="preview_url">
        </div>

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">中文预览</button>
            <input type="text" name="chinese-preview-url" placeholder="中文预览地址" class="form-control" id="chinese_preview_url">
        </div>

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">后台</button>
        <input type="text" name="dashboard-url" placeholder="后台网址" class="form-control" id="dashboard_url">
        </div>

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">扫毒网址</button>
        <input type="text" name="virus-scan-url" placeholder="病毒扫描网址" class="form-control" id="virus_report">
        </div>

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">头图</button>
        <input type="text" name="featured_image" placeholder="头图" class="form-control" id="featured_image">
        </div>

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">分类ID</button>
        <input type="text" name="category" placeholder="分类ID，多ID用英文逗号隔开" class="form-control" id="category">
        </div>

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">标签</button>
        <input type="text" name="tags" class="form-control" placeholder="请输入标签，多标签用逗号隔开" id="tags">
        </div>

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">版本号</button>
        <input type="text" name="version" class="form-control" placeholder="请输入版本" id="version">
        </div>

        <textarea name="content" class="form-control mb-4" placeholder="请输入描述" id="content" rows="10">
            <h3>功能特色</h3>
               <ul>
                  <li></li>
                  <li></li>
                  <li></li>
                  <li></li>
               </ul>
            <h3>后台截图</h3>
            <strong>更多后台选项可以点击上面的后台预览按钮登录进行预览。</strong>
        </textarea>

        <div class="input-group mb-4">
            <button class="btn btn-secondary" type="button" disabled="disabled">产品图片1</button>
            <input type="text" name="product_image1" placeholder="请输入产品图片1" id="product_image1" class="form-control">
        </div>

        <input type="text" name="product_image2" placeholder="请输入产品图片2" id="product_image2" class="form-control mb-4">

        <input type="text" name="product_image3" placeholder="请输入产品图片3" id="product_image3" class="form-control mb-4">

        <input type="text" name="product_image4" placeholder="请输入产品图片4" id="product_image4" class="form-control mb-4">

        <input type="text" name="product_image5" placeholder="请输入产品图片5" id="product_image5" class="form-control mb-4">

        <input type="text" name="product_image6" placeholder="请输入产品图片6" id="product_image6" class="form-control mb-4">

		<input type="submit" value="提交" class="btn btn-primary text-bold mb-5" name="submitForm" id="submitBtn">

	</form>
</div>

<script>

    // Function to get URL parameter value by name
    function getURLParameter(name) {
        const queryString = window.location.search;
        const urlParams = new URLSearchParams(queryString);
        return urlParams.get(name);
    }

    document.getElementById('submitBtn').addEventListener('click', function(event){
         event.preventDefault();
         const pass = document.getElementById('pass').value

        if(pass === ''){
            alert('password missing');
            return;
        }

        const url = document.getElementById('data-url').value

        if(url === ''){
            alert('url missing');
            return;
        }

        document.getElementById('submit-form').submit();

    })

    document.getElementById('product_name').addEventListener('change', function(){
        let product_name_lowercase = document.getElementById('product_name').value.toLowerCase()
        let product_name_permalink = product_name_lowercase.replaceAll(' ', '-')
        let version = document.getElementById('version').value.trim()
        document.getElementById('permalink').value = product_name_permalink
        document.getElementById('chinese_preview_url').value = "https://"+product_name_permalink+".wpdemo.top"
        document.getElementById('dashboard_url').value = "https://"+product_name_permalink+".wpdemo.top/wp-login.php"
        document.getElementById('download_uri').value = "https://wpdemo.top/downloads/plugins/"+product_name_permalink+"-"+version+".zip"
        document.getElementById('tags').value = document.getElementById('product_name').value
        document.getElementById('product_image1').value = document.getElementById('featured_image').value
    })

    document.getElementById('get-data').addEventListener('click', function (){

        if(getURLParameter('pass') == null){
            alert('password missing');
            return;
        }

        const url = document.getElementById('data-url').value

        if(url === ''){
            alert('url missing');
            return;
        }

        //send a post request using fetch to this page
        fetch('/wp-content/themes/ripro-v5/extra/submit/process-request.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                url: url
            })
        }).then(response => response.json())
            .then(data => {
                console.log(data);
                const result = data.data
                document.getElementById('title').value = result.title
                document.getElementById('preview_url').value = result.sales_url
                document.getElementById('featured_image').value = result.featured_image
                document.getElementById('vendor_url').value = result.sales_url
                document.getElementById('virus_report').value = result.virus_report
                document.getElementById('version').value = result.version
                document.getElementById('product_name').value = result.title
                document.getElementById('short_description').value = result.description
            })
            .catch((error) => {
                console.error('Error:', error);
            });
    });
</script>

</body>
</html>
